﻿Public Class SROOMITEM
    Dim PROVIDER_Check As New PROVIDERSCLASS
    Dim connect As CONDB = CONDB.NewConnection()
    Dim dtsetSroomitem As DataSet
    Dim s2 As FILTERCLASS

    Private Sub filterClinic()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(clinic AS CHAR(6)) AS clinic, clinicname, cliniccode FROM masclinic WHERE status = '1' AND f_wrd = 1"
        s = New FILTERCLASS(txtClinic, sql, "idแผนก,แผนก,codeแผนก", "80,200,50", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Private Sub filterCCode()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(ccode AS CHAR(6)) AS ccode, name FROM finchcode WHERE status = '1'"
        s = New FILTERCLASS(txtIccode, sql, "รหัสCcode,Ccode", "80,200", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Private Sub filterRCode()
        Dim sql As String = "SELECT CAST(rcode AS CHAR(6)) AS rcode, name FROM finrecode WHERE status = '1'"
        s2.SetSql = sql
    End Sub

    Private Sub filterRCodeByCcode()
        Dim sql As String = "SELECT CAST(rcode AS CHAR(6)) AS rcode, name FROM finrecode WHERE status = '1' AND ccode = '" & txtIccode.Tag & "'"
        s2.SetSql = sql
    End Sub

    Private Sub filterBUnit()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(unitid AS CHAR(6)) AS unitid, unitname_th, unitname_en FROM masunit WHERE status = '1'"
        s = New FILTERCLASS(txtB_unit, sql, "IDหน่วย,หน่วยไทย,หน่วยอังกฤษ", "80,200,200", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Private Sub filterSUnit()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(unitid AS CHAR(6)) AS unitid, unitname_th, unitname_en FROM masunit WHERE status = '1'"
        s = New FILTERCLASS(txtS_unit, sql, "IDหน่วย,หน่วยไทย,หน่วยอังกฤษ", "80,200,200", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub querySroomitem()
        dtsetSroomitem = New DTSETSROOMITEM

        Dim objGetSroomitem As New SROOMITEMCLASS
        Dim sql As String = objGetSroomitem.selectSroomitem_
        connect.GetTable(sql, dtsetSroomitem.Tables("sroomitemJoinMasproduct"))

        connect.Dispose()

        xgvSroomitem.DataSource = dtsetSroomitem
        xgvSroomitem.ForceInitialize()
    End Sub

    Private Sub SROOMITEM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        querySroomitem()
        
        filterClinic()
        filterCCode()
        filterBUnit()
        filterSUnit()
        'filterRCode()

        Dim sql As String = "SELECT CAST(rcode AS CHAR(6)) AS rcode, name FROM finrecode WHERE status = '1'"
        s2 = New FILTERCLASS(txtIrcode, sql, "รหัสRcode,Rcode", "80,200", "1,1", "1,1")
        s2.SetShowBorder = True

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub txtIccode_TextChanged(sender As Object, e As EventArgs) Handles txtIccode.TextChanged
        If Convert.ToString(txtIccode.Tag) <> "" Then
            filterRCodeByCcode()
        Else
            filterRCode()
        End If
    End Sub

    Private Sub GridView1_RowClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridView1.RowClick
        txtBedsid.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("bedsid").ToString()
        txtClinic.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("clinic").ToString()
        txtClinic.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("clinicname").ToString()
        txtPrdcode.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("prdcode").ToString()
        txtBedscode.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("bedscode").ToString()
        txtBedsname.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("bedsname").ToString()
        txtRemark.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("remark").ToString()

        txtIccode.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("iccode").ToString()
        txtIccode.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("icname").ToString()

        txtIrcode.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("ircode").ToString()
        txtIrcode.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("irname").ToString()

        txtB_unit.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("b_unit").ToString()
        txtB_unit.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("buyUnit").ToString()

        txtS_unit.Tag = GridView1.GetDataRow(GridView1.FocusedRowHandle)("s_unit").ToString()
        txtS_unit.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("sellUnit").ToString()

        txtIpdprc.Text = GridView1.GetDataRow(GridView1.FocusedRowHandle)("ipdprc").ToString()

        If GridView1.GetDataRow(GridView1.FocusedRowHandle)("status") IsNot DBNull.Value Then
            ckbSroomStatus.Checked = Convert.ToBoolean(GridView1.GetDataRow(GridView1.FocusedRowHandle)("status"))
        Else
            ckbSroomStatus.Checked = False
        End If

        linePrdcode.Visible = True
        lblPrdcode.Visible = True
        txtPrdcode.Visible = True
    End Sub

    Private Sub btnAdddata_Click(sender As Object, e As EventArgs) Handles btnAdddata.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If txtClinic.Tag IsNot Nothing And txtClinic.Tag <> "" And txtBedscode.Text.Length.ToString > 0 And txtBedsname.Text.Length.ToString > 0 Then
                    Dim objAddmasproduct As New MASPRODUCT
                    objAddmasproduct.CODE_ = txtBedscode.Text.Trim
                    objAddmasproduct.PRDNAME_ = txtBedsname.Text.Trim

                    If txtIccode.Tag <> "" Then
                        objAddmasproduct.ICCODE_ = txtIccode.Tag
                    End If

                    If txtIrcode.Tag <> "" Then
                        objAddmasproduct.IRCODE_ = txtIrcode.Tag
                    End If

                    If txtB_unit.Tag <> "" Then
                        objAddmasproduct.B_UNIT_ = txtB_unit.Tag
                    End If

                    If txtS_unit.Tag <> "" Then
                        objAddmasproduct.S_UNIT_ = txtS_unit.Tag
                    End If

                    If txtIpdprc.Text <> "" Then
                        objAddmasproduct.IPDPRC_ = txtIpdprc.Text.Trim
                    End If

                    objAddmasproduct.PRDCAT_ = 3
                    objAddmasproduct.MPDSTATUS_ = ckbSroomStatus.Checked

                    Dim sqlMasproduct As String = objAddmasproduct.makeMasproductInsertSQL

                    Dim objAddSroomitem As New SROOMITEMCLASS
                    objAddSroomitem.clinic_ = txtClinic.Tag
                    objAddSroomitem.bedscode_ = txtBedscode.Text.Trim
                    objAddSroomitem.bedsname_ = txtBedsname.Text.Trim
                    objAddSroomitem.stid_ = 1
                    objAddSroomitem.remark_ = txtRemark.Text.Trim
                    objAddSroomitem.masproductSQL_ = sqlMasproduct

                    If objAddSroomitem.insertSROOMITEM = True Then
                        querySroomitem()
                        clearForm()

                        linePrdcode.Visible = False
                        lblPrdcode.Visible = False
                        txtPrdcode.Visible = False
                    End If
                Else
                    MsgBox("ท่านต้องระบุข้อมูล คลินิก, รหัสเตียง, ชื่อเตียง")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub btnEditData_Click(sender As Object, e As EventArgs) Handles btnEditData.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลห้องพักผู้ป่วยดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                If editStat = 1 Then
                    If txtBedsid.Text.Length.ToString > 0 And txtPrdcode.Text.Length.ToString > 0 And txtClinic.Tag IsNot Nothing And txtClinic.Tag <> "" And txtBedscode.Text.Length.ToString > 0 And txtBedsname.Text.Length.ToString > 0 Then
                        Dim objEditmasproduct As New MASPRODUCT
                        objEditmasproduct.MPDPRDCODE_ = txtPrdcode.Text

                        If txtIccode.Tag <> "" Then
                            objEditmasproduct.ICCODE_ = txtIccode.Tag
                        End If

                        If txtIrcode.Tag <> "" Then
                            objEditmasproduct.IRCODE_ = txtIrcode.Tag
                        End If

                        If txtB_unit.Tag <> "" Then
                            objEditmasproduct.B_UNIT_ = txtB_unit.Tag
                        End If

                        If txtS_unit.Tag <> "" Then
                            objEditmasproduct.S_UNIT_ = txtS_unit.Tag
                        End If

                        If txtIpdprc.Text <> "" Then
                            objEditmasproduct.IPDPRC_ = txtIpdprc.Text.Trim
                        End If


                        objEditmasproduct.PRDCAT_ = 3

                        objEditmasproduct.CODE_ = txtBedscode.Text.Trim
                        objEditmasproduct.PRDNAME_ = txtBedsname.Text.Trim
                        objEditmasproduct.MPDSTATUS_ = ckbSroomStatus.Checked

                        Dim sqlMasproduct As String = objEditmasproduct.makeMasproductEditSQL

                        Dim objEditSroomitem As New SROOMITEMCLASS
                        objEditSroomitem.bedsid_ = txtBedsid.Text
                        objEditSroomitem.clinic_ = txtClinic.Tag
                        objEditSroomitem.bedscode_ = txtBedscode.Text.Trim
                        objEditSroomitem.bedsname_ = txtBedsname.Text.Trim
                        objEditSroomitem.stid_ = 1
                        objEditSroomitem.remark_ = txtRemark.Text.Trim
                        objEditSroomitem.masproductSQL_ = sqlMasproduct

                        If objEditSroomitem.editSROOMITEM = True Then
                            querySroomitem()
                            clearForm()

                            linePrdcode.Visible = False
                            lblPrdcode.Visible = False
                            txtPrdcode.Visible = False
                        End If
                    Else
                        MsgBox("ท่านต้องระบุข้อมูล คลินิก, รหัสเตียง, ชื่อเตียง")
                    End If
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("sroomitem")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("sroomitem")
        nextform.ShowDialog()
    End Sub

    Private Sub clearForm()
        txtClinic.Tag = ""
        txtClinic.Clear()
        txtBedscode.Clear()
        txtBedsname.Clear()
        txtRemark.Clear()
        txtPrdcode.Clear()
        txtIccode.Tag = ""
        txtIccode.Clear()
        txtIrcode.Tag = ""
        txtIrcode.Clear()
        txtB_unit.Tag = ""
        txtB_unit.Clear()
        txtS_unit.Tag = ""
        txtS_unit.Clear()
        txtIpdprc.Text = ""
        ckbSroomStatus.Checked = False
        txtBedsid.Clear()
    End Sub

   
End Class